package com.ruoyi.system.controller;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.NewDateUtils;
import com.ruoyi.system.domain.AlpStageActual;
import com.ruoyi.system.domain.AlpStageBudget;
import com.ruoyi.system.domain.AlpStagePredict;
import com.ruoyi.system.domain.MonthDetail;
import com.ruoyi.system.service.IAlpStageActualService;
import com.ruoyi.system.service.IAlpStageBudgetService;
import com.ruoyi.system.service.IAlpStagePredictService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IMonthDetailService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 月份详情表Controller
 * 
 * @author zhangjun
 * @date 2020-02-19
 */
@Controller
@RequestMapping("/system/monthDetail")
public class MonthDetailController extends BaseController
{
    private String prefix = "system/monthDetail";

    @Autowired
    private IMonthDetailService monthDetailService;

    @Autowired
    private IAlpStageActualService alpStageActualService;

    @Autowired
    private IAlpStageBudgetService alpStageBudgetService;

    @Autowired
    private IAlpStagePredictService alpStagePredictService;

    private BigDecimal multiplyNum = new BigDecimal(100);
    private BigDecimal zero = new BigDecimal(0);

    @RequiresPermissions("system:monthDetail:view")
    @GetMapping()
    public String monthDetail()
    {
        return prefix + "/monthDetail";
    }

//    /**
//     * 查询月份详情表列表
//     */
//    @RequiresPermissions("system:monthDetail:list")
//    @PostMapping("/list")
//    @ResponseBody
//    public TableDataInfo list(MonthDetail monthDetail)
//    {
//        startPage();
//        List<MonthDetail> list = monthDetailService.selectMonthDetailList(monthDetail);
//        return getDataTable(list);
//    }

    /**
     * 查询月份详情表列表
     */
//    @GetMapping("/listByStageId/{stageId}")
    @PostMapping("/listByStageId/{stageId}")
    @ResponseBody
    public TableDataInfo listByStageId(@PathVariable("stageId") String stageId)
    {
        startPage();
        Long stageIdStr = Long.valueOf(stageId);
        //循环12个月 填入各月金额
        List<AlpStageBudget> budgets = alpStageBudgetService.selectAlpStageBudgetByStageId(stageIdStr);
        List<AlpStagePredict> predicts = alpStagePredictService.selectAlpStagePredictByStageId(stageIdStr);
        List<AlpStageActual> actuals = alpStageActualService.selectAlpStageActualByStageId(stageIdStr);
        List<MonthDetail> monthDetailList = new ArrayList<>();
        for (int i=1; i<=12; i++) {
            MonthDetail monthDetail = new MonthDetail();

            BigDecimal budgetMoney = new BigDecimal(0);
            BigDecimal predictMoney = new BigDecimal(0);
            BigDecimal actualMoney = new BigDecimal(0);
            //累加本月金额   详细时间没办法累加 所以去掉了
            for (AlpStageBudget budget: budgets) {
                Date budgetTime = budget.getBudgetTime();
                if (i == NewDateUtils.getMonth(budgetTime)) {
                    budgetMoney = budgetMoney.add(budget.getBudgetMoney()) ;
                }
            }
            for (AlpStagePredict predict: predicts) {
                Date predictTime = predict.getPredictTime();
                if (i == NewDateUtils.getMonth(predictTime)) {
                    predictMoney = predictMoney.add(predict.getPredictMoney()) ;
                }
            }
            for (AlpStageActual actual: actuals) {
                Date actualTime = actual.getActualTime();
                if (i == NewDateUtils.getMonth(actualTime)) {
                    actualMoney = actualMoney.add(actual.getActualMoney()) ;
                }
            }
            monthDetail.setSeqNo("" + i);
            monthDetail.setBudgetMoney(budgetMoney);
            monthDetail.setPredictMoney(predictMoney);
            monthDetail.setActualMoney(actualMoney);

            monthDetail.setFinishRateBudget(calcRate(actualMoney, budgetMoney));
            monthDetail.setFinishRatePredict(calcRate(actualMoney, predictMoney));

            //余额  = 上个月余额+新签单-实际


            monthDetailList.add(monthDetail);
        }

        return getDataTable(monthDetailList);
    }

    public String calcRate(BigDecimal fenzi, BigDecimal fenmu) {
        if (fenmu.equals(zero)) {
            return "--";
        }
        BigDecimal result = fenzi.multiply(multiplyNum).divide(fenmu, 2, RoundingMode.HALF_UP);
        return result + "%";
    }

    /**
     * 应收表 展示月份详情跳转
     */
    @GetMapping("/listMonth/{stageId}")
    public String listMonth(@PathVariable("stageId") String stageId, ModelMap mmap)
    {

        mmap.put("stageId", stageId);
        return prefix + "/detailList";
    }

    /**
     * 导出月份详情表列表
     */
    @RequiresPermissions("system:monthDetail:export")
    @Log(title = "月份详情表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(MonthDetail monthDetail)
    {
        List<MonthDetail> list = monthDetailService.selectMonthDetailList(monthDetail);
        ExcelUtil<MonthDetail> util = new ExcelUtil<MonthDetail>(MonthDetail.class);
        return util.exportExcel(list, "monthDetail");
    }

    /**
     * 新增月份详情表
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存月份详情表
     */
    @RequiresPermissions("system:monthDetail:add")
    @Log(title = "月份详情表", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(MonthDetail monthDetail)
    {
        return toAjax(monthDetailService.insertMonthDetail(monthDetail));
    }

    /**
     * 修改月份详情表
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        MonthDetail monthDetail = monthDetailService.selectMonthDetailById(id);
        mmap.put("monthDetail", monthDetail);
        return prefix + "/edit";
    }

    /**
     * 修改保存月份详情表
     */
    @RequiresPermissions("system:monthDetail:edit")
    @Log(title = "月份详情表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MonthDetail monthDetail)
    {
        return toAjax(monthDetailService.updateMonthDetail(monthDetail));
    }

    /**
     * 删除月份详情表
     */
    @RequiresPermissions("system:monthDetail:remove")
    @Log(title = "月份详情表", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(monthDetailService.deleteMonthDetailByIds(ids));
    }
}
